﻿using System.IO;

namespace Baby.VideoConvertor
{
    public static class FlvInfoHelper
    {
        public static FlvInfo Read(string Path)
        {
            using (FileStream fs = File.OpenRead(Path))
            {
                FlvInfo aFlvInfo = new FlvInfo();
                fs.Read(aFlvInfo.Header.Type, 0, aFlvInfo.Header.Type.Length);
                aFlvInfo.Header.Version = (byte)fs.ReadByte();
                aFlvInfo.Header.Stream = (byte)fs.ReadByte();
                fs.Read(aFlvInfo.Header.Length, 0, aFlvInfo.Header.Length.Length);
                byte[] previoustagsize = new byte[4];
                while (fs.Read(previoustagsize, 0, previoustagsize.Length) != 0)
                {
                    FLVTag Tag = new FLVTag();
                    Tag.PreviousTagSize = previoustagsize;
                    Tag.Type = (byte)fs.ReadByte();
                    fs.Read(Tag.DataLength, 0, Tag.DataLength.Length);
                    fs.Read(Tag.Time, 0, Tag.Time.Length);
                    Tag.TimeEx = (byte)fs.ReadByte();
                    fs.Read(Tag.streamsID, 0, Tag.streamsID.Length);

                    byte[] tmp = new byte[4];
                    tmp[3] = 0;
                    tmp[2] = Tag.DataLength[0];
                    tmp[1] = Tag.DataLength[1];
                    tmp[0] = Tag.DataLength[2];
                    int n = System.BitConverter.ToInt32(tmp, 0);
                    fs.Seek(n, SeekOrigin.Current);
                    aFlvInfo.TagList.Add(Tag);

                }
                return aFlvInfo;
            }
        }
    }
}
